{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 正则re"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 正则类及方法\n",
    "|对象、方法、模式|描述|\n",
    "|----|----|\n",
    "|Pattern正则对象|re.compile创建正则对象，方便复用|\n",
    "|Match匹配对象|该对象可以查看匹配成功后的组、正则表达式字符串等信息，如果匹配不成功则为None|\n",
    "|re.split方法|类似s.split,但支持多个分隔符。例如：s = 'aa;bb cc dd', re.split(r';竖\\s'),返回['aa','bb','cc','dd']|\n",
    "|re.findall方法|以列表方式返回所有匹配到的字符串。例如：s = 'Aaaaa Bxxxx dfrswfgvs'，re.findall(r'A\\\\w+竖B\\\\w+',s)， 返回['Aaaaa', 'Bxxxx']|\n",
    "|re.search方法|返回match对象，该对象包含大量匹配到的信息。例如：s = '2016-1-2', match = re.search(r'(\\d\\*)-(\\d\\*)-(\\d\\*)',s), match.group(0)等于s, match.group(1)等于'2016',match.group(2)等于'1',match.group(3)等于'2',|\n",
    "|re.finditer方法|返回match对象的迭代器，其他同search函数|\n",
    "|re.sub方法|替换匹配的字符串。例如：s = '2016-1-2'，re.sub(r'(\\d*)-(\\d*)-(\\d*)', r'\\g左尖括号2右尖括号/\\g左尖括号3右尖括号/\\g左尖括号1右尖括号',s) #1/2/2016，如果使用命名分组(?P<year>),则使用\\g左尖括号year右尖括号|\n",
    "|re.S单行模式|使 . 匹配包括换行在内的所有字符。例如：s = \"\"\"AAA\\nBBB\"\"\"，re.findall(r\"\"\"AAA.*BBB\"\"\",s, re.S)，不带re.S返回[]|\n",
    "|re.M多行模式|在多行模式下，每行都有^\\$,否则所有文本只有一个^\\$|\n",
    "|re.I大小写模式|匹配时，忽略大小写模式。例如：re.findall(r'abc', 'Abc', re.I) #['Abc']，不带re.I返回[]|\n",
    "|re.X注释模式|匹配时，忽略空格符和#注释。例如：re.findall(r\"\"\"Abc                   #aaaaaa\"\"\", 'Abc', re.X)，不带re.X语法错误|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aa', 'bb', 'cc', 'dd']\n",
      "--------------\n",
      "['Aaaaa', 'Bxxxx']\n",
      "--------------\n",
      "####2016-1-2\n",
      "<_sre.SRE_Pattern object at 0x000000000394F4D0>\n",
      "0\n",
      "12\n",
      "3\n",
      "None\n",
      "('2016', '1', '2')\n",
      "{}\n",
      "--------------\n",
      "2016-1-2\n",
      "2016\n",
      "1\n",
      "2\n",
      "--------------\n",
      "4\n",
      "4\n",
      "9\n",
      "11\n",
      "--------------\n",
      "12\n",
      "8\n",
      "10\n",
      "12\n",
      "--------------\n",
      "(4, 8)\n",
      "--------------\n",
      "1/2/2016\n",
      "--------------\n",
      "day\n",
      "{'year': '2016', 'mon': '1', 'day': '2'}\n",
      "1/2/2016\n",
      "--------------\n",
      "####1/2/2016\n",
      "--------------\n",
      "[]\n",
      "['Abc']\n",
      "--------------\n",
      "['abc']\n",
      "['abc', 'abc']\n",
      "--------------\n",
      "[]\n",
      "['AAA\\nBBB']\n",
      "--------------\n",
      "['AAA\\nBBB']\n",
      "--------------\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "s = 'aa;bb cc dd'\n",
    "print re.split(r';|\\s',s)\n",
    "\n",
    "print '--------------'\n",
    "s = 'Aaaaa Bxxxx dfrswfgvs'\n",
    "print re.findall(r'A\\w+|B\\w+',s)\n",
    "\n",
    "print '--------------'\n",
    "s = '####2016-1-2'\n",
    "match = re.search(r'(\\d*)-(\\d*)-(\\d*)',s)\n",
    "print match.string     #匹配时使用的文本\n",
    "print match.re         #正则对象\n",
    "print match.pos         #开始查找位置\n",
    "print match.endpos      #结束查找位置\n",
    "print match.lastindex   #最后一个被捕获的分组在文本中的索引，也就是group(3)的下标\n",
    "print match.lastgroup   #最后一个被捕获的分组的别名。如果这个分组没有别名或者没有被捕获的分组，将为None\n",
    "print match.groups()    #返回所有捕获组捕获到的信息\n",
    "print match.groupdict() #返回所有有别名捕获组的信息，如果没有任何别名的捕获组，返回None\n",
    "print '--------------'\n",
    "print match.group(0)    #捕获组0，捕获的字符串\n",
    "print match.group(1)    #捕获组1，捕获的字符串\n",
    "print match.group(2)    #捕获组2，捕获的字符串\n",
    "print match.group(3)    #捕获组3，捕获的字符串\n",
    "print '--------------'\n",
    "print match.start(0)    #捕获组0，捕获字符串开始位置\n",
    "print match.start(1)    #捕获组1，捕获字符串开始位置\n",
    "print match.start(2)    #捕获组2，捕获字符串开始位置\n",
    "print match.start(3)    #捕获组3，捕获字符串开始位置\n",
    "print '--------------'\n",
    "print match.end(0)      #捕获组0，捕获字符串结束位置\n",
    "print match.end(1)      #捕获组1，捕获字符串结束位置\n",
    "print match.end(2)      #捕获组2，捕获字符串结束位置\n",
    "print match.end(3)      #捕获组3，捕获字符串结束位置\n",
    "print '--------------'\n",
    "print match.span(1)     #捕获组1，(捕获字符串开始位置,捕获字符串结束位置)\n",
    "print '--------------'\n",
    "print match.expand(r'\\g<2>/\\g<3>/\\g<1>')  #重组捕获组的字符串信息\n",
    "print '--------------'\n",
    "match = re.search(r'(?P<year>\\d*)-(?P<mon>\\d*)-(?P<day>\\d*)',s)\n",
    "print match.lastgroup\n",
    "print match.groupdict()\n",
    "print match.expand(r'\\g<mon>/\\g<day>/\\g<year>')  #重组捕获组的字符串信息\n",
    "\n",
    "print '--------------'\n",
    "print re.sub(r'(?P<year>\\d*)-(?P<mon>\\d*)-(?P<day>\\d*)', r'\\g<mon>/\\g<day>/\\g<year>', s) #匹配后替换\n",
    "\n",
    "print '--------------'\n",
    "s = 'Abc'\n",
    "print re.findall(r'abc',s)\n",
    "print re.findall(r'abc',s, re.I)   #大小写模式\n",
    "\n",
    "print '--------------'\n",
    "s = '''abc\n",
    "abc'''\n",
    "print re.findall(r'^abc',s)\n",
    "print re.findall(r'^abc',s,re.M)   #多行模式\n",
    "\n",
    "print '--------------'\n",
    "s = \"\"\"AAA\n",
    "BBB\"\"\"\n",
    "print re.findall(r\"\"\"AAA.*BBB\"\"\",s)\n",
    "print re.findall(r\"\"\"AAA.*BBB\"\"\",s, re.S) #单行模式\n",
    "\n",
    "print '--------------'\n",
    "s = \"\"\"AAA\n",
    "BBB\"\"\"\n",
    "print re.findall(r\"\"\"AAA.*BBB       #aaaaaaaaaaaaa\"\"\",s, re.S | re.X) #注释模式\n",
    "\n",
    "print '--------------'\n",
    "s = 'CCCC2016-1-2'\n",
    "print re.findall(r'(?=C)\\d',s)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
